package com.qianya.system.manage.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.pojo.sys.vo.SysSetLimitIpVO;
import com.qianya.system.manage.config.RedisUtil;
import com.qianya.system.manage.web.sys.model.SysSetLimitIp;
import com.qianya.system.manage.web.sys.service.ISysSetLimitIpService;
import com.qianya.system.manage.web.sys.mapper.SysSetLimitIpMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.qianya.model.common.ResultCode;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
*  前台ip访问限制服务实现类
* </p>
*
* @author lhl
* @since 2020-06-11
*/
@Slf4j
@Service
public class SysSetLimitIpServiceImpl extends ServiceImpl<SysSetLimitIpMapper, SysSetLimitIp> implements ISysSetLimitIpService {

        /**
        * 修改 前台ip访问限制
        * @author lhl
        * @since 2020-06-11
        */
        @Override
        public CommonResult renewal(SysSetLimitIp sysSetLimitIp, HttpServletRequest request) {

            remove(new QueryWrapper<SysSetLimitIp>().ne("FI_ID",""));

            boolean b = save(sysSetLimitIp);
            if (b){
                ipLimitInit();
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }
        /**
         * @desc ip限制初始化
         * @author lhl
         * @date 2020-07-07 15:07
         * @return
        */
        @Override
        public void ipLimitInit() {
            List<SysSetLimitIp> list= list();
            List<SysSetLimitIpVO> listObj=new ArrayList<SysSetLimitIpVO>();
            SysSetLimitIpVO vo=null;
            for (int i = 0; i < list.size(); i++) {
                vo=new SysSetLimitIpVO();
                vo.setFiBegin(list.get(i).getFiBegin());
                vo.setFiEnd(list.get(i).getFiEnd());
                vo.setFiId(list.get(i).getFiId());
                listObj.add(vo);
        }
            RedisUtil.set("ipLimit",listObj);
            log.info("ip段限制缓存初始化...");
        }
}
